<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="学校名称">
          <a-input placeholder="请输入学校名称" v-decorator="['schoolName', validatorRules.notnull]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="校级分类">
          <a-select placeholder="请输入校级分类" v-decorator="['schoolTeachLv', validatorRules.notnull]">
            <a-select-option value="">请选择校级分类</a-select-option>
            <a-select-option value="0">幼儿园</a-select-option>
            <a-select-option value="1">学前班</a-select-option>
            <a-select-option value="2">小学</a-select-option>
            <a-select-option value="3">初中</a-select-option>
            <a-select-option value="4">高中</a-select-option>
            <a-select-option value="5">大学</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="学校地址">
          <a-input placeholder="请输入学校地址" v-decorator="['address', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="经度">
          <a-input placeholder="请输入经度" v-decorator="['longitude', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="纬度">
          <a-input placeholder="请输入纬度" v-decorator="['latitude', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="图片64编码">
          <a-input placeholder="请输入图片64编码" v-decorator="['picBase64', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="学校范围">
          <a-input placeholder="请输入学校范围" v-decorator="['schoolRange', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="颜色代码">
          <a-input placeholder="请输入颜色代码" v-decorator="['schoolRangeColour', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="删除标识0-正常,1-已删除">
          <a-input placeholder="请输入删除标识0-正常,1-已删除" v-decorator="['delFlag', validatorRules.delFlag ]" />
        </a-form-item>
      </a-form>
      <modal :show="show" :title="diotitle" @hideModal="hideModal" @submit="submit">
        <p>这里放弹窗的内容</p>
      </modal>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import Modal from '@/views/snMap/mapDio.vue'
  import moment from "moment"

  export default {
    name: "SchoolModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 },
        },
        imgSrc:require("../img/map.png"),
        show:true,
        diotitle:'弹窗标题',
        components: {
          Modal
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        delFlag:{rules: [{ required: true, message: '请输入删除标识0-正常,1-已删除!' }]},
        notnull:{rules: [{ required: true, message: '此项不可为空!' }]},
        },
        url: {
          add: "/school/school/add",
          edit: "/school/school/edit",
        },
      }
    },
    created () {
    },
    methods: {
      hideModal() {
        // 取消弹窗回调
        this.show = false
      },

      submit() {
        // 确认弹窗回调
        this.show = false
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'schoolName','schoolTeachLv','address','longitude','latitude','picBase64','schoolRange','schoolRangeColour','delFlag'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>